package controllers;


import models.tutor.EnrollUnit;
import models.tutor.Theme;
import models.tutor.Tutor;
import play.modules.router.Get;
import play.mvc.*;

import java.util.*;

import services.tutor.TutorService;
import utils.JSON;
import utils.query.Page;

public class Application extends Controller {

    @Get("/tts/index")
    public static void index() {
        render();
    }

    @Get("/tts/unitJson")
    public static void unitJson() {
        List<EnrollUnit> units = TutorService.getUnits();
        renderJSON(JSON.customExpose().toJson(units));
    }

    @Get("/tts/schoolList/{schoolName}")
    public static void schoolList(String schoolName){
        render(schoolName);
    }

    @Get("/tts/nameList/{name}")
    public static void nameList(String name){
        render(name);
    }

    @Get("/tts/schoolJson/{name}")
    public static void schoolJson(String name,String tutorType,int page) {
        Page<Tutor> tutors = TutorService.tutorList(name,tutorType,page);
        renderJSON(JSON.excludeWithoutExpose().toJson(tutors));
    }

    @Get("/tts/nameJson/{name}")
    public static void nameJson(String name,int page) {
        Page<Tutor> tutors = TutorService.tutorListByMultiple(name, page);
        renderJSON(JSON.excludeWithoutExpose().toJson(tutors));
    }

    @Get("/tts/tutor/detail/{code}")
    public static void detail(String code) {
        render(code);
    }

    @Get("/tts/tutor/json/{code}")
    public static void jsonDetail(String code) {
        Tutor tutor = TutorService.getTutorByCypheredCode(code);
        if (tutor==null || !tutor.isOpen)
            renderJSON("{}");
        else
            renderJSON(JSON.customExpose().toJson(tutor));
    }

    @Get("/tts/themes/json/{code}")
    public static  void jsonTheme(String code){
        List<Theme> themes = TutorService.getThemesByCode(code);
        renderJSON(JSON.customExpose().toJson(themes));
    }

    @Get("/monitor")
    public static void monitor() {
        try{
            if(Tutor.count()>0){
                renderText("+ok");
            }else{
                error(503,"不能访问");
            }
        }catch(Exception e){
            error(503,"不能访问");
        }
    }
}